package evi.com.evi.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import evi.com.evi.R;
import evi.com.evi.activity.BusinessVolActivity;
import evi.com.evi.activity.GoodsManagerActivity;
import evi.com.evi.activity.LeafMessageActivity;
import evi.com.evi.activity.UserInfoActivity;
import evi.com.evi.application.MyApplication;
import evi.com.evi.utils.ChannelStatusConsts;
import evi.com.evi.utils.ContentUtils;
import evi.com.evi.utils.HeartBreakUtil;
import evi.com.evi.utils.JSONUtils;
import evi.com.evi.utils.SPUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ManagerFragment extends Fragment implements OnClickListener{
	private View view;

	private RelativeLayout rl_01;
	private RelativeLayout rl_02;
	private RelativeLayout rl_03;
	private RelativeLayout rl_04;
	private RelativeLayout rl_05;
	private Activity home;

	private ImageView iv_next1;
	private boolean isSwitch = true;

	private OkHttpClient client;
	private JSONUtils jsonUtil;

//	public ManagerFragment(Activity activity){
//		this.home = activity;
//	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_manager, container,false);
		initView();
		loadSwitchStatus();
		return view;
	}

	public void loadSwitchStatus(){
		if( client == null){
			client = new OkHttpClient();
		}
		if( jsonUtil == null){
			jsonUtil = new JSONUtils();
		}
		Request request = new Request.Builder()
				.addHeader("token", SPUtil.getString(MyApplication.getContext(), "token"))
				.url(ContentUtils.getMerchantInfo_url)
				.build();

		client.newCall(request).enqueue(new Callback() {
			@Override
			public void onFailure(Call call, IOException e) {
//				Log.e(TAG, "查询商户信息失败" + e);
			}

			@Override
			public void onResponse(Call call, Response response) throws IOException {
				String jsonData = response.body().string();
//				Log.e("dd","------------->>>>>>>>>>>>>>"+jsonData);
				if (jsonUtil.validate(jsonData)) {
					JSONObject jsonObject = JSON.parseObject(jsonData);
					if (jsonObject.getBoolean("success")) {
						final Integer status = jsonObject.getJSONObject("data").getInteger("status");
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if( status != null && status == 8001){
									isSwitch = true;
									iv_next1.setImageResource(R.drawable.ic_switch_on);
								}else{
									isSwitch = false;
									iv_next1.setImageResource(R.drawable.ic_switch_off);
								}
							}
						});
					}
				}
//				Log.e("d", "查询总订单联网成功" + jsonData);
			}

		});
	}

	private void initView() {

		if( client == null){
			client = new OkHttpClient();
		}

		rl_01 = (RelativeLayout) view.findViewById(R.id.rl_01);
		rl_02 = (RelativeLayout) view.findViewById(R.id.rl_02);
		rl_03 = (RelativeLayout) view.findViewById(R.id.rl_03);
		rl_04 = (RelativeLayout) view.findViewById(R.id.rl_04);
		rl_05 = (RelativeLayout) view.findViewById(R.id.rl_05);
		rl_01.setOnClickListener(this);
		rl_02.setOnClickListener(this);
		rl_03.setOnClickListener(this);
		rl_04.setOnClickListener(this);
		rl_05.setOnClickListener(this);
		iv_next1 = (ImageView) view.findViewById(R.id.iv_next1);
		iv_next1.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.rl_01:		//是否营业

				break;

			case R.id.iv_next1:		//开关
				if(isSwitch){
					isSwitch = false;
					iv_next1.setImageResource(R.drawable.ic_switch_off);
					HeartBreakUtil.setWorkStack(client, null, ChannelStatusConsts.CHANNEL_STATUS_REST);
//					Log.e("EEE","switch === off");
				}else{
					isSwitch = true;
//					Log.e("EEE","switch === on");
					HeartBreakUtil.setWorkStack(client, null, ChannelStatusConsts.CHANNEL_STATUS_WORK);
					iv_next1.setImageResource(R.drawable.ic_switch_on);
				}
				break;
			case R.id.rl_02:		//商品管理
				startActivity(new Intent(getActivity(), GoodsManagerActivity.class));
				break;

			case R.id.rl_03:		//商家信息
				startActivity(new Intent(getActivity(), UserInfoActivity.class));
				break;

			case R.id.rl_04:		//留言评价
				startActivity(new Intent(getActivity(), LeafMessageActivity.class));
				break;

			case R.id.rl_05:		//营业额
				startActivity(new Intent(getActivity(), BusinessVolActivity.class));
				break;
			default:
				break;
		}
	}
}
